package com.shitou.shitoumember.service.impl;

import com.alibaba.fastjson.JSON;
import com.shitou.shitoucommons.exception.RRException;
import com.shitou.shitoucommons.utils.RespBean;
import com.shitou.shitoucommons.vo.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.Query;

import com.shitou.shitoumember.dao.MemberReceiveAddressDao;
import com.shitou.shitoumember.entity.MemberReceiveAddressEntity;
import com.shitou.shitoumember.service.MemberReceiveAddressService;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Slf4j
@SuppressWarnings("all")
@Service("memberReceiveAddressService")
public class MemberReceiveAddressServiceImpl extends ServiceImpl<MemberReceiveAddressDao, MemberReceiveAddressEntity> implements MemberReceiveAddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberReceiveAddressEntity> page = this.page(
                new Query<MemberReceiveAddressEntity>().getPage(params),
                new QueryWrapper<MemberReceiveAddressEntity>()
        );

        return new PageUtils(page);
    }


    /**
     * 添加用户地址
     * @param umsMemberReceiveAddress
     * @return
     */
    @Override
    public RespBean addAddress(MemberReceiveAddressEntity umsMemberReceiveAddress) {
        // 会员最多只能添加只有20条数据，查询该会员的所有地址信息
        List<MemberReceiveAddressEntity> addressList = baseMapper.selectList(
                new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", umsMemberReceiveAddress.getMemberId()));
        int size = addressList.size();
        if ( size >= 20 ){
            log.info("您的地址以及超额了,请删除不常用的地址");
            throw new RRException("您的地址以及超额了,请删除不常用的地址");
        }

        // 检查添加数据的时候是否设置为默认地址
        // 判断默认地址
        checkDefaultStatus(umsMemberReceiveAddress);

        // 满足进行收货地址的添加
        baseMapper.insert(umsMemberReceiveAddress);

        return RespBean.success();
    }

    /**
     * 修改默认地址
     * @param umsMemberReceiveAddress
     * @return
     */
    @Override
    public RespBean setDefaultAddress(MemberReceiveAddressEntity umsMemberReceiveAddress) {
        // 判断默认地址
        checkDefaultStatus(umsMemberReceiveAddress);
        // 修改当前的
        baseMapper.updateById(umsMemberReceiveAddress);
        return RespBean.success();
    }

    /**
     * 获得会员用户所有的地址
     * @param umsMemberReceiveAddress
     * @return
     */
    @Override
    public RespBean getMemberAddress(MemberReceiveAddressEntity umsMemberReceiveAddress) {
        List<MemberReceiveAddressEntity> addressList = baseMapper.selectList(
                new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", umsMemberReceiveAddress.getMemberId()));

        return RespBean.success().put("data", addressList);
    }

    /**
     * 更新默认地址
     * @param umsMemberReceiveAddress
     * @return
     */
    @Override
    public RespBean updateAddress(MemberReceiveAddressEntity umsMemberReceiveAddress) {
        // 判断默认地址
        checkDefaultStatus(umsMemberReceiveAddress);
        baseMapper.updateById(umsMemberReceiveAddress);
        return RespBean.success();
    }

    @Override
    public RespBean deleteAddress(MemberReceiveAddressEntity umsMemberReceiveAddress) {
        baseMapper.deleteById(umsMemberReceiveAddress.getId());

        return RespBean.success();
    }

    /**
     * 远程调用，获取用户的地址信息
     * @param addressId
     * @param memberId
     * @return
     */
    @Override
    public RespBean getAddressVo(long addressId, long memberId) {
        AddressVo addressVo = new AddressVo();
        MemberReceiveAddressEntity address = this.getOne(new QueryWrapper<MemberReceiveAddressEntity>().eq("id", addressId));
        if ( address == null ){
            throw  new RRException("address_id is exception at getAddressVo");
        }
        if ( address.getMemberId() != memberId ){
            throw new RRException("该地址不是当前登录会员地址");
        }
        addressVo.setName(address.getName());
        addressVo.setPhone(address.getPhone());
        addressVo.setProvince(address.getProvince());
        addressVo.setCity(address.getCity());
        addressVo.setRegion(address.getRegion());
        addressVo.setDetailAddress(address.getDetailAddress());
        addressVo.setPostCode(address.getPostCode());

        String jsonString = JSON.toJSONString(addressVo);
        return RespBean.success().put("data", jsonString);
    }


    /**
     * 远程调用，返回该地址是否是该用户的
     * @param memberId
     * @param addressId
     * @return
     */
    @Override
    public RespBean checkMemberAddress(Long memberId, Long addressId) {
        // 通过用户id和地址id和status获取用户地址，
        MemberReceiveAddressEntity receiveAddressEntity = this.getOne(new QueryWrapper<MemberReceiveAddressEntity>().eq("member_id", memberId)
                .eq("id", addressId));
        if ( receiveAddressEntity == null ){
            return RespBean.success().put("data", "false");
        }
        // 是自己的地址
        return RespBean.success().put("data", "true");
    }



    public void checkDefaultStatus(MemberReceiveAddressEntity umsMemberReceiveAddress){
        // 传过来的数据并没有设置为默认地址，则返回
        Integer defaultStatus = umsMemberReceiveAddress.getDefaultStatus();
        if ( defaultStatus != 1 ){
            return ;
        }
        // 将该用户的之前设置的默认地址进行清空
        MemberReceiveAddressEntity receiveAddressDo = this.getOne(new QueryWrapper<MemberReceiveAddressEntity>()
                .eq("member_id", umsMemberReceiveAddress.getMemberId())
                .eq("default_status", 1));
        if ( receiveAddressDo != null ){
            // 修改
            receiveAddressDo.setDefaultStatus(0);
            this.updateById(receiveAddressDo);
        }
    }

}
